package com.lastproject.ejb.util;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import sun.org.mozilla.javascript.internal.IdScriptableObject;

import com.lastproject.dto.ConfidentialEmployeeDTO;
import com.lastproject.ejb.dao.RoleDAORemote;
import com.lastproject.ejb.entities.ConfidentialEmployee;
import com.lastproject.ejb.entities.Department;
import com.lastproject.ejb.entities.Role;
import com.lastproject.ejb.entities.User;

/**
 * Session Bean implementation class ConfidentialEmployeeConversion Author:
 * Alexandra
 */
@Stateless
@LocalBean
public class ConfidentialEmployeeConversion {

	@PersistenceContext
	EntityManager entityManager;
	
//	@EJB
//	RoleConversion roleConv;
//	
//	@EJB
//	RoleDAORemote roleDAO;

	public ConfidentialEmployeeConversion() {

	}
	
	

	public ConfidentialEmployeeDTO fromEntity(ConfidentialEmployee ce) {

		ConfidentialEmployeeDTO cedto = new ConfidentialEmployeeDTO();

		cedto.setId(ce.getId());
		cedto.setUsername(ce.getUsername());
		cedto.setPassword(ce.getPassword());
		cedto.setStatusPassword(ce.getStatusPassword());
		cedto.setFirstname(ce.getFirstname());
		cedto.setLastname(ce.getLastname());
		cedto.setEnabled(ce.isEnabled());
		cedto.setDob(ce.getDob());
		cedto.setPersonalEmail(ce.getPersonalEmail());
		cedto.setPersonalPhone(ce.getPersonalPhone());
		cedto.setEmploymentDate(ce.getEmploymentDate());
		cedto.setDepartureDate(ce.getDepartureDate());
		cedto.setMedCheckUpDate(ce.getMedCheckUpDate());
		cedto.setEmploymentType(ce.getEmploymentType());
		cedto.setPosition(ce.getPosition());
		cedto.setCv(ce.getCv());

		cedto.setProsisNr(ce.getProsisNr());
		cedto.setWorkEmail(ce.getWorkEmail());
		cedto.setWorkPhone(ce.getWorkPhone());
		cedto.setGermanContactPerson(ce.getGermanContactPerson());
		cedto.setCareerLevel(ce.getCareerLevel());
		
		cedto.setSalary(ce.getSalary());
		cedto.setVarPart(ce.getVarPart());
		
		try {
			cedto.setIdDepartment(ce.getDepartment().getId());
		} catch(Exception ex) {
			System.out.println("Exception caught for department >:)");
		}
		
		try {
			cedto.setIdSuperior(ce.getSuperior().getId());
		} catch(Exception ex) {
			System.out.println("Exception caught for superior >:)");
		}

		// created by erik
		try {
			cedto.setIdTeamLeader(ce.getTeamLeader().getId());
		} catch(Exception ex) {
			System.out.println("Exception caught for team lead >:)");
		}
		
		return cedto;
	}

	public User fromDTO(ConfidentialEmployeeDTO cedto) {
		
		

		ConfidentialEmployee ce = entityManager.find(
				ConfidentialEmployee.class, cedto.getId());

		if (ce == null) {
			ce = new ConfidentialEmployee();
		}

		ce.setId(cedto.getId());
		ce.setUsername(cedto.getUsername());
		ce.setPassword(cedto.getPassword());
		ce.setStatusPassword(cedto.getStatusPassword());
		ce.setFirstname(cedto.getFirstname());
		ce.setLastname(cedto.getLastname());
		ce.setEnabled(cedto.isEnabled());
		ce.setDob(cedto.getDob());
		ce.setPersonalEmail(cedto.getPersonalEmail());
		ce.setPersonalPhone(cedto.getPersonalPhone());
		ce.setEmploymentDate(cedto.getEmploymentDate());
		ce.setDepartureDate(cedto.getDepartureDate());
		ce.setMedCheckUpDate(cedto.getMedCheckUpDate());
		ce.setEmploymentType(cedto.getEmploymentType());
		ce.setPosition(cedto.getPosition());
		ce.setCv(cedto.getCv());

		ce.setProsisNr(cedto.getProsisNr());
		ce.setWorkEmail(cedto.getWorkEmail());
		ce.setWorkPhone(cedto.getWorkPhone());
		ce.setGermanContactPerson(cedto.getGermanContactPerson());
		ce.setCareerLevel(cedto.getCareerLevel());

		ce.setDepartmentManager(cedto.isDepartmentManager());
		
		ce.setSalary(cedto.getSalary());
		ce.setVarPart(cedto.getVarPart());

		// created by erik
		//role + team lead ->>>> refactor
		ce.setTeamLeader(entityManager.find(ConfidentialEmployee.class,
				cedto.getIdTeamLeader()));
		ce.setTeamLeader(cedto.isTeamLeader());
		
		
		ce.setDepartment(entityManager.find(Department.class,
				cedto.getIdDepartment()));
		
		int idSuperior = cedto.getIdSuperior();
		try{	
			ce.setSuperior(entityManager.find(ConfidentialEmployee.class,
					idSuperior));
			
			System.out.println("in ce conversion, identified superior is: " + ce.getSuperior());
		}catch(Exception ex) {
			System.out.println("In ce conv caught exception");
			System.out.println("In ce conv," + idSuperior);
		}

		return ce;
	}

	public List<Role> convertRolesFromDTO(ConfidentialEmployeeDTO cedto) {
		
		List<Role> roleList = new ArrayList<Role>();
		
		for(int i=0; i<cedto.getRolesIdList().size(); i++) {
//			System.out.println(i + " iterations so far");
			Role auxRole = entityManager.find(Role.class, cedto.getRolesIdList().get(i));
//			System.out.println("in iteration " + i + " role is " + auxRole.getName());
			roleList.add(auxRole);
		}
		
		return roleList;
	}
	
	
	public List<Integer> convertRolesFromEntity(ConfidentialEmployee ce) {
		
		List<Integer> roleList = new ArrayList<>();
		
		for(int i=0; i<ce.getRoles().size(); i++) {		
			
			int idRole = ce.getRoles().get(i).getId();					
			roleList.add(idRole);
		}
		
		return roleList;
	}


}
